//
//  YGKJLoginVC.m
//  新项目
//
//  Created by 刘耀宗 on 2016/10/24.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "YGKJLoginVC.h"
#import "YGKJTabbarVC.h"

@interface YGKJLoginVC ()<UITextFieldDelegate,UIScrollViewDelegate>
@property (nonatomic, strong) UITextField *acountTextField;
@property (nonatomic, strong) UITextField  *passWordField;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *btn;
@property (strong, nonatomic) UIAlertView *netAlert;

@end

@implementation YGKJLoginVC
@synthesize netAlert = _netAlert;



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"商家登录";
    [self.view addSubview:self.scrollView];
    self.scrollView.backgroundColor=[UIColor groupTableViewBackgroundColor];
//    self.scrollView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    self.scrollView.frame=self.view.bounds;
    [self.scrollView sd_addSubviews:@[self.acountTextField, self.passWordField,self.loginBtn,self.imageView,self.btn]];
    [self prepareUI];
    [self.acountTextField becomeFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated {
    // 清除登录信息
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    [loginDefaults removeObjectForKey:@"loginInfo"];
    [loginDefaults synchronize];
    self.navigationItem.hidesBackButton = YES;
}

- (void)prepareUI {
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 16, 60)];
    view.backgroundColor=[UIColor clearColor];
    
    UIView *view2=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 16, 60)];
    view2.backgroundColor=[UIColor clearColor];
    self.imageView.sd_layout
    .topSpaceToView(self.scrollView,28+64)
    .centerXEqualToView(self.scrollView)
    .widthIs(150)
    .heightIs(120);
    
    self.acountTextField.sd_layout
    .topSpaceToView(self.imageView,34)
    .leftSpaceToView(self.scrollView,32)
    .rightSpaceToView(self.scrollView,32)
    .heightIs(60);
    self.acountTextField.backgroundColor=CcColor(245, 245, 245);
    self.acountTextField.placeholder=@"请输入账号";
    self.acountTextField.leftView=view2;
    self.acountTextField.leftViewMode=UITextFieldViewModeAlways;

    self.passWordField.sd_layout
    .centerXEqualToView(self.scrollView)
    .leftEqualToView(self.acountTextField)
    .topSpaceToView(self.acountTextField,20)
    .rightEqualToView(self.acountTextField)
    .heightIs(60);
    self.passWordField.placeholder=@"请输入密码";
    self.passWordField.backgroundColor=CcColor(245, 245, 245);
    self.passWordField.leftView=view;
    self.passWordField.leftViewMode=UITextFieldViewModeAlways;
    
    self.btn.sd_layout
    .centerYEqualToView(self.passWordField)
    .rightSpaceToView(self.scrollView,32+10)
    .widthIs(20)
    .heightIs(20);
    
    self.loginBtn.sd_layout
    .centerXEqualToView(self.scrollView)
    .topSpaceToView(self.passWordField,16)
    .leftSpaceToView(self.scrollView,32)
    .rightSpaceToView(self.scrollView,32)
    .heightIs(42);
    
    [self.scrollView setupAutoContentSizeWithBottomView:self.loginBtn bottomMargin:20];
}

#pragma mark---点击登录
- (void)clickForLogin {
    if (self.acountTextField.text.length==0) {
        [MBProgressHUD showError:@"请输入您的账号"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            // 隐藏登录指示器
            [MBProgressHUD hideHUD];
        });return;
    }
    if (self.passWordField.text.length==0) {
        [MBProgressHUD showError:@"请输入您的密码"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            // 隐藏登录指示器
            [MBProgressHUD hideHUD];
        });
        return;
    }
    
    [self loginNetRequest];
}

- (void)clickForLook {
    self.passWordField.secureTextEntry=!self.passWordField.secureTextEntry;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIImageView *)imageView {
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image=[UIImage imageNamed:@"ic_logo"];
    }
    return _imageView;
}

- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.delegate=self;
        
    }
    return _scrollView;
}

- (UITextField *)acountTextField {
    if (_acountTextField == nil) {
        _acountTextField = [[UITextField alloc] init];
        _acountTextField.placeholder=@"   请输入您的账号";
        _acountTextField.delegate=self;
        _acountTextField.leftViewMode=UITextFieldViewModeAlways; //此处用来设置
        _acountTextField.text=@"xiaoz";
        _acountTextField.layer.borderWidth=1;
        _acountTextField.layer.borderColor=[CcColor(245, 245, 245) CGColor];
        _acountTextField.layer.cornerRadius=5;
        _acountTextField.layer.masksToBounds=YES;
        _acountTextField.font=newFont(14);
    }
    return _acountTextField;
}

- (UITextField *)passWordField {
    if (_passWordField == nil) {
        _passWordField = [[UITextField alloc] init];
        _passWordField.placeholder=@"   请输入您的密码";
        _passWordField.text=@"123456";
        _passWordField.delegate=self;
         _passWordField.leftViewMode=UITextFieldViewModeAlways; //此处用来设置
        _passWordField.layer.borderWidth=1;
        _passWordField.layer.borderColor=[CcColor(245, 245, 245) CGColor];
        _passWordField.layer.cornerRadius=5;
        _passWordField.layer.masksToBounds=YES;
        _passWordField.font=newFont(14);
        _passWordField.secureTextEntry=YES;
    }
    return _passWordField;
}

- (UIButton *)loginBtn {
    if (_loginBtn == nil) {
        _loginBtn = [[UIButton alloc] init];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setBackgroundColor:CcColor(17, 128, 184)];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(clickForLogin) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.layer.cornerRadius=5;
        _loginBtn.layer.masksToBounds=YES;
        _loginBtn.titleLabel.font=newFont(18);
    }
    return _loginBtn;
}

- (UIButton *)btn {
    if (_btn == nil) {
        _btn = [[UIButton alloc] init];
        [_btn setBackgroundImage:[UIImage imageNamed:@"ic_logcode"] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(clickForLook) forControlEvents:UIControlEventTouchUpInside];
        
        [_btn sizeToFit];
    }
    return _btn;
}

#pragma mark - 登录网络请求
- (void)loginNetRequest {
    NSMutableDictionary *dataDict = [[NSMutableDictionary alloc] init];
    [dataDict setObject:_acountTextField.text forKey:@"LoginName"];
    [dataDict setObject:_passWordField.text forKey:@"PassWord"];
    NSString *postURL = @"api/CmpPerson/Login/ToLogin";
    NSString *jsonStr = [WHJSON jsonStringWithObject:dataDict];
    NSMutableDictionary *paramsDict=[NSMutableDictionary dictionary];
    [paramsDict setObject:jsonStr forKey:@"data"];
    
    [MBProgressHUD showMessage:@"请稍候..."];
    [YBHttpTool post:postURL params:paramsDict success:^(NSDictionary *resultObj){
        if(resultObj){
            NSInteger theCode = [[resultObj objectForKey:@"code"] integerValue];
            if(theCode){
                NSDictionary *sucDict = [WHJSON objectFormJsonString:[resultObj objectForKey:@"resultJson"]];
                sucDict = [DMToolsObject replaceNullObjctWithDict:sucDict];
                // 缓存登录信息
                NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
                [loginDefaults setObject:sucDict forKey:@"loginInfo"];
                [loginDefaults synchronize];
                
                
                [self presentViewController:[[YGKJTabbarVC alloc] init] animated:YES completion:nil];

            }else{
                NSString *messageStr = [resultObj objectForKey:@"message"];
                if(messageStr == nil || [messageStr isEqualToString:@""]){
                    messageStr = @"登录失败！";
                }
                [self showAlertActionWith:messageStr];
            }
        }else{
            NSString *messageStr = @"登录失败！";
            [self showAlertActionWith:messageStr];
        }
        [MBProgressHUD hideHUD];
    
    } failure:^(NSError *error){
        [MBProgressHUD hideHUD];
        NSString *messageStr = @"请求失败！";
        [self showAlertActionWith:messageStr];
    }];
}

- (void)showAlertActionWith:(NSString *)theAlertMsg {
    if(!_netAlert){
        _netAlert = [[UIAlertView alloc] initWithTitle:nil message:theAlertMsg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    }
    _netAlert.message = theAlertMsg;
    [_netAlert show];
}


@end
